// pages/management/weixin_register.js
var app = getApp()
var srte = null
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    }
    },
  getUserInfo: function(e) {//用户登录
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
    wx.request({//检验用户是否已注册
      url: app.globalData.getUserInfo_url,
      data: {
        code: 'censor',
        openid: wx.getStorageSync('openid')
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (res) {
        console.log(res)
        if(res.data.error == 200){
          wx.setStorageSync('topic',res.data.state)
        }else{
          wx.request({//未注册进行注册
            url: app.globalData.getUserInfo_url,
            data: {
              code: 'egister',
              openid: wx.getStorageSync('openid')
            },
            header: {
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            success: function (res) {
              console.log(res)
              wx.setStorageSync('topic',res.data.state)
            }
        })
        }
      }
  }) 
  },
  devicesupervise:function () {
    console.log('sda')
    wx.navigateTo({
      url: '../devicesupervise/devicesupervise',
    })
  },
  add_device:function () {
    console.log('sda')
    wx.navigateTo({
      url: '../add_device/add_device',
    })
  },
  _out:function(){
  wx.clearStorage()
  this.onLoad()
  app.globalData.userInfo={}
  console.log('sda')
}


})